var app = getApp();
var ux = require('../../../../lib/ux.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    city: '',
    province: '',
    pam:{},
    images:[],
    isShow:true,
    merchant:null,
    next: true,
    list: [],
    page: 0,
    size:10,
    agentCard:{},
    agentUrl:"/api/memberAgent/getBanrdOneAgent.do",
    action: "/api/memberShop/goodsLists.do",
    submit: "{{submit}}"
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      banrdId: wx.getStorageSync("banrdId"),
      pam: wx.getStorageSync("parameter"),
      images: wx.getStorageSync("parameter").listMap ? wx.getStorageSync("parameter").listMap.split(","):[],
    })
    if(this.data.isShow){
      this.setData({
        isShow: false, 
        next: true,
        list: [],
        page: 0,
        size: 10,
        agentCard: {},
      })
   
      this.getList();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({
      isShow:true
    })
  },
  gotoChong: function (event) {
    var dataset = event.currentTarget.dataset;
    var index=dataset.index;
    wx.navigateTo({
      url: '/pages/member/tab3/recharge/recharge?id=' + this.data.list[index].id+'&tab=1',
    })
  },
  gotDetails: function (event){
    var dataset = event.currentTarget.dataset;
    var index = dataset.index;
    wx.navigateTo({
      url: '/pages/member/tab1/shop/detail?prices=' + this.data.list[index].price+'&id=' + this.data.list[index].id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.userId){
      this.setData({
        userId:options.userId,
        city: options.city,
        province: options.province,
        merchant: options.banrdId
      })
      wx.setStorageSync('banrdId', options.banrdId)
      var self=this;
      var parmasaa = {
        merchant: options.banrdId,
        memberId: options.userId
      }
      ux.redLogin(parmasaa, function (protocol) {
      var banrdid=wx.getStorageSync('banrdId');
      ux.request("/api/Merchant/getShars.do", {banrd:parseInt(banrdid),type:1},function(data){
        if(data.data.obj){
          console.log(JSON.stringify(data.data.obj))
          self.setData({
            fenxiang:data.data.obj
          })
        }
      });
    })
    }else{
      this.setData({
        city: options.city,
        province: options.province,
        merchant: wx.getStorageSync("banrdId")
      })
      var self=this;
      var banrdid=wx.getStorageSync('banrdId');
      ux.request("/api/Merchant/getShars.do", {banrd:parseInt(banrdid),type:1},function(data){
        if(data.data.obj){
          console.log(JSON.stringify(data.data.obj))
          self.setData({
            fenxiang:data.data.obj
          })
        }
      });
    }
  },
  getList: function () {
    var self = this;
    if (self.data.next == false) {
      wx.showToast({
        title: '没有更多了',
        icon: 'loading',
        duration: 1500
      });
      return;
    }
    wx.showLoading({
      mask: true,
      title: '加载中....'
    })
    var params = { city: this.data.city, province: this.data.province,merchant:self.data.merchant,pageNo: parseInt(self.data.page), pageSize: self.data.size };
    ux.request(self.data.action, params, function (data) {
      var datas = data.data.list;
      if (data.data.pageInfo.next == true) {
        self.setData({
          next: true
        });
      } else {
        self.setData({
          next: false
        });
      }
      if (self.data.page == 0) {
        self.setData({
          list: datas
        });
      } else {
        self.setData({
          list: self.data.list.concat(datas)
        });
      }
      var pages = self.data.page + 1;
      self.setData({
        page: pages
      });
      setTimeout(function () {
        wx.hideLoading()
      }, 500)
    });
  },
  onForm: function (event) {
    if (this.data.submit) {
      var $this = this;
      var dataset = event.currentTarget.dataset;
      ux.request(this.data.submit, detail.value, function (protocol) {
        if (protocol.success) {
          if (dataset.url) {
            wx.redirectTo(dataset.url);
          }
        }
      }, true);
    }
  },

  onTap: function (event) {
    var dataset = event.currentTarget.dataset;
    var data = this.data;
    if (dataset.key && dataset.val) {
      data[dataset.key] = dataset.val;
    }
    this.setData(data);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getList();
  },

  /**
    * 用户点击右上角分享
    */
  onShareAppMessage: function () {
    var self = this;
    wx.showShareMenu({
      withShareTicket: true
    })
    var shareObj = {
      title: this.data.fenxiang.shareTitle,
      imageUrl: this.data.fenxiang.url,
      path: '/pages/member/tab1/shop/yplist?userId=' + wx.getStorageSync("userInfo").uid+"&city="+this.data.city+"&province="+this.data.province+"&banrdId="+this.data.merchant,
      success: function (res) {
        console.log(JSON.stringify(res));
      },
      fail: function () {
        wx.showToast({
          title: '转发失败',
        })
      }
    }
    return shareObj;
  },
})
